import socket
import socks

class cyConn():
	oPorts = []
	services = []
	result = []
	proxyHost = "127.0.0.1"
	proxyPort = 9050
	
	def __init__(self):
		prxConn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		try:
			prxConn.connect((self.proxyHost, self.proxyPort))
		except:
			exit("proxy connection error")
		
		socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, self.proxyHost, self.proxyPort, True)
		self.sock = socks.socksocket()

	def isOn(self):
		socket.socket = socks.socksocket
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		try:
			self.socket.sendto((self.target, 7))
			return True
		except:
			return False

	def tcpConn(self, port):
		socket.socket = socks.socksocket
		TCPsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		TCPsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		TCPsock.settimeout(2.0)
		try:
			#if self.isOn():
			TCPsock.connect((self.target, port))
				#self.services.append(TCPsock.getservbyport(port, 'tcp'))
			return True
		except:
			return None

	def mpScan(self, target, port=None, prange=None):
		if not port and not prange or not target:
			return "function usage: mpScan((string) target, (int) port or (list) range))"

		self.target = target

		if prange and port is None:
			for p in prange:
				print(p)
				if self.tcpConn(p):
					self.oPorts.append(p)
		elif port and prange is None:
			if self.tcpConn(port):
				self.oPorts.append(port)
		else:
			return "function usage: mpScan((string) target, (int) port or (list) range))"
		self.result = [self.oPorts, self.services]
		return self.result

c = cyConn()
r = c.mpScan(target="137.74.187.102", prange=[80, 443, 8080])
print("rslt =\n\t{}".format(r))
